package com.sczmgk.hotline_pzh.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sczmgk.hotline_pzh.MyApp;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.adapter.MyNewsHomeAdapter;
import com.sczmgk.hotline_pzh.base.RefreshAndLoadMoreBaseActivity;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.entity.InformationBean;
import com.sczmgk.hotline_pzh.sp.CacheUtils;
import com.sczmgk.hotline_pzh.view.NoDoubleItemClickListener;
import com.sczmgk.hotline_pzh.view.RefreshListView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/14 0014.
 */

public class SearchActivity extends RefreshAndLoadMoreBaseActivity<InformationBean> implements View.OnClickListener {


    private Button mSearch;
    /*更多详情发送的消息*/
    private static final int MORE_DETAIL_INFO = 100;
    /*搜索内容*/
    private EditText mEtSearchContent;

    /*显示的内容Adapter*/
    private MyNewsHomeAdapter adapter;
    private LinearLayout mLayoutEmpty;
    private int type;
    private String moduleTitle;
    private TextView mTvEmpty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        setHeader();

        initView();

        initData();

        setListener();

    }

    /**
     * 设置头
     */
    private void setHeader() {

        Intent intent = getIntent();
        //获取模块类型,默认为0(搜索全部)
        type = intent.getIntExtra("type", 0);
        moduleTitle = intent.getStringExtra(Constants.SEARCH_MODULE_TITLE);

        String search = getResources().getString(R.string.search);
        if (moduleTitle != null) {
            setTitle(moduleTitle + search);
        }else{
            setTitle(search);
        }
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化控件
     */
    private void initView() {
        //搜索内容输入框
        mEtSearchContent = (EditText) findViewById(R.id.et_search_content);

        //显示内容的Listview
        mListView = (RefreshListView) findViewById(R.id.lv_search_content);

        //搜索的按钮
        mSearch = (Button) findViewById(R.id.bt_search);

        //空图标的展示
        mLayoutEmpty = (LinearLayout) findViewById(R.id.ll_search_empty);

        //搜索无内容的文本
        mTvEmpty = (TextView) findViewById(R.id.tv_search_empty);
    }

    /**
     * 初始化数据
     */
    private void initData() {

        //设置adapter
        adapter = new MyNewsHomeAdapter(SearchActivity.this);

        mListView.setAdapter(adapter);
    }

    /**
     * 设置监听
     */
    @Override
    public void setListener() {
        super.setListener();
        mSearch.setOnClickListener(this);

        mListView.setOnItemClickListener(new NoDoubleItemClickListener() {

            @Override
            public void NoDoubleItemClickListener(AdapterView<?> parent, View view, int i, long id) {

                int newPosition = i - 1 ;
                InformationBean informationBean = lists.get(newPosition);
                Intent intent = new Intent(SearchActivity.this, NewsDetailActivity.class);
                intent.putExtra(Constants.NEWS_DETAIL_ITEM, informationBean);
//                intent.putExtra(Constants.MY_COLLECTION_FLAG, Constants.MY_COLLECTION_FLAG_INFO);
                startActivity(intent);
            }
        });

        setLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    @Override
    public Map<String, Object> getRequestData(int page, String keyword) {

        String inputKeyword = mEtSearchContent.getText().toString().trim();

        Map<String, Object> maps = new HashMap<>();
        maps.put("imei", MyApp.imei);
        maps.put("page", page);
        maps.put("keyword", inputKeyword);
        maps.put("userid", CacheUtils.getIntData(SearchActivity.this, Constants.LOGIN_UID, 0));
        maps.put("type", type);

        return maps;
    }

    @Override
    public String getRquestUrl() {
        return Constants.BASE_URL + Constants.GET_SEARCH_INFORMATION;
    }

    @Override
    public int getListType() {
        return LIST_TYPE_UP_LOAD_MORE;
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case REFRESH_LIST:

                if (lists.size() > 0){
                    mLayoutEmpty.setVisibility(View.GONE);
                    mListView.setEnablePullDown(true);
                }else{
                    mLayoutEmpty.setVisibility(View.VISIBLE);
                    mTvEmpty.setVisibility(View.VISIBLE);
                    mListView.setEnablePullDown(false);
                }

                adapter.setLists(lists);
                adapter.notifyDataSetChanged();

                break;
        }
    }

    @Override
    public boolean getIsNeedLogin() {
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_search :

                loadData(1, "" , LOAD_NORMAL);

                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            setResult(RESULT_OK);
            finish();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }

    }
}
